/**
 * @Author: Caven
 * @Date: 2020-05-07 19:28:33
 */
import $ from 'jquery';
class ViewerApi {
  constructor(viewer) {
    this._viewer = viewer
  }

  get viewer() {
    return this._viewer
  }

  addBaseLayer() {
    let baidu = DC.ImageryLayerFactory.createAmapImageryLayer({
      style: 'img'
    })
    this._viewer.addBaseLayer(baidu)
    return this
  }

  scenemap() {
    this._viewer.setOptions({
      showMoon: false,
      showSun: false,
      enableFaxx: true,
      globe: {
        show: false
      },
      skyBox: {
        show: false
      }
    })
    this._viewer.use(new DC.Effect())
    // this._viewer.effect.night.enable = true
    // this._viewer.effect.bloom.brightness = 0.1

    this.loadVLine()
    this._viewer.zoomToPosition(
      '120.6119943479279,30.237286794313203,126132.920,0,-48.84',
      // '117.005343,36.667922,296132.920,0,-90',
      () => {
        this.loadSzData()
        this.loadSzOutline()
        this.loadSzPolygon()
        this.loadSzLine()
        this.loadSzZxd()
      }
    )
  }

  loadVLine() {
    let layer = new DC.PrimitiveLayer('vLine_layer')
    this._viewer.addLayer(layer)
    let positions = [
      { lng: 119.61305431791536, lat: 31.949257981346843 },
      { lng: 119.66748528834533, lat: 31.569088949848066 },
      { lng: 119.93621458239951, lat: 30.862978381864128 },
      { lng: 120.33269379413352, lat: 30.779032494324515 },
      { lng: 120.91846175667051, lat: 30.772412670398687 },
      { lng: 121.37081537125513, lat: 31.031108498202467 },
      { lng: 121.68788865110909, lat: 31.59956942613548 },
      { lng: 121.31821229420665, lat: 32.00645809208013 }
    ]
    positions.forEach(item => {
      let position = DC.Position.fromObject(item)
      let end = position.copy()
      end.alt = 80000 * Math.random()
      let line = new DC.FlowLinePrimitive([position, end], 0.5)
      line.setStyle({
        speed: 8,
        color: DC.Color.fromCssColorString('rgb(240,131,61)'),
        percent: 0.2,
        gradient: 0.01
      })
      layer.addOverlay(line)
    })
  }
  loadSzData() {
    szData = new DC.GeoJsonLayer('sz', 'sz.geojson')
    sz_out_data = new DC.GeoJsonLayer('sz-out', 'sz_out.geojson')
    // szData = new DC.GeoJsonLayer('sz', 'xian_3701_org.geojson')
    // sz_out_data = new DC.GeoJsonLayer('sz-out', 'shi_3701.geojson')
  }
  async loadSzZxd() {
    let layer_sz_zxd = new DC.VectorLayer('layer-sz-zxd').addTo(this._viewer)
    let geo = await $.getJSON('xian_3701_org.geojson');
// console.log(turf.centroid(geo.features[0]))
const { turf } = DC.Namespace;
// sz_zxd_data = geo.features.map(ft=>({label:ft.properties.name,coord:turf.centroid(ft).geometry.coordinates[0]+','+turf.centroid(ft).geometry.coordinates[1]}));
    console.log(sz_zxd_data)
    sz_zxd_data.forEach(item => {
      let position = DC.Position.fromString(item.coord)
      position.alt = 3000
      let billboard = new DC.Billboard(
        position,
        'bg_name.png'
      )
      billboard.size = [item.label.length * 25, 32]
      billboard.setStyle({
        scale: 0.8,
        distanceDisplayCondition: {
          near: 0,
          far: 2.5e5
        }
      })
      billboard.setLabel(item.label, {
        font: '10px',
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
        distanceDisplayCondition: {
          near: 0,
          far: 2.5e5
        }
      })
      let circle = new DC.Circle(position, 2000)
      circle.setStyle({
        height: 2000,
        material: new DC.CircleWaveMaterialProperty({
          speed: 5,
          color: DC.Color.fromCssColorString('rgb(205,100,33)')
        })
      })
      layer_sz_zxd.addOverlay(billboard).addOverlay(circle)
    })
  }

  loadSzPolygon() {
    this._viewer.addLayer(sz_out_data)
    sz_out_data.eachOverlay(entity => {
      DC.Util.merge(entity.polygon, {
        outline: false,
        material: DC.Color.fromCssColorString('rgb(47,50,54)'),
        extrudedHeight: 2000
      })
    })
  }

  loadSzOutline() {
    let layer_sz_outline = new DC.VectorLayer('layer-sz-outline').addTo(
      this._viewer
    )
    let now = DC.JulianDate.now()
    sz_out_data.eachOverlay(entity => {
      if (entity.polygon) {
        let positions = DC.Transform.transformCartesianArrayToWGS84Array(
          entity.polygon.hierarchy.getValue(now).positions
        )
        positions.map(item => {
          item.alt = 2000
        })
        let polyline = new DC.Polyline(positions)
        polyline.setStyle({
          width: 2,
          material: new DC.PolylineTrailMaterialProperty({
            speed: 2,
            color: DC.Color.fromCssColorString('rgb(149,236,217)')
          })
        })
        layer_sz_outline.addOverlay(polyline)
      }
    })
  }

  loadSzLine() {
    let layer_sz = new DC.VectorLayer('layer-sz-line').addTo(this._viewer)
    let now = DC.JulianDate.now()
    szData.eachOverlay(entity => {
      if (entity.polygon) {
        let positions = DC.Transform.transformCartesianArrayToWGS84Array(
          entity.polygon.hierarchy.getValue(now).positions
        )
        positions.map(item => {
          item.alt = 2000
        })
        let polyline = new DC.Polyline(positions)
        polyline.setStyle({
          width: 1,
          material: new DC.PolylineFlickerMaterialProperty({
            speed: 10 * Math.random(),
            color: DC.Color.fromCssColorString('rgb(116,173,250)')
          })
        })
        layer_sz.addOverlay(polyline)
      }
    })
  }
}

export default ViewerApi

let szData = undefined
let sz_out_data = undefined
let sz_zxd_data = [
  {
    label: '姑苏区',
    coord: '120.59665360,31.32417637'
  },
  {
    label: '吴中区',
    coord: '120.26656151,31.12600545'
  },
  {
    label: '吴江区',
    coord: '120.61407203,31.00940314'
  },
  {
    label: '相城区',
    coord: '120.61658599,31.44896501'
  },
  {
    label: '虎丘区',
    coord: '120.42240336,31.35921238'
  },
  {
    label: '工业园区',
    coord: '120.73975003,31.33381359'
  },
  {
    label: '昆山市',
    coord: '120.96280041,31.31506478'
  },
  {
    label: '太仓市',
    coord: '121.15395177,31.57029622'
  },
  {
    label: '常熟市',
    coord: '120.81050568,31.66691104'
  },
  {
    label: '张家港市',
    coord: '120.61839073,31.90941293'
  }
]
